package cn.wolfcode.controller;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Region;
import cn.wolfcode.query.RegionQuery;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IRegionService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")RegionQuery qo){
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page", page);
        return "/region/list";
    }

    @RequestMapping("/listByAll")
    @ResponseBody
    public Object listByAll(){
        return destinationService.findAll();
    }



    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){

        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        regionService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        List<Destination> list = destinationService.queryDestByRegionId(rid);
        return list;
    }


    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot, String id){
        destinationService.updateIsHotById(hot, id);
        return JsonResult.success();
    }
}
